<?php


namespace app\admin\controller;


use think\admin\Controller;

/**
 * 云存储管理
 * Class CloudStorage
 * @package app\admin\controller
 */
class CloudStorage extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'lgCloudStorage';

    /**
     * 云存储列表
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        //标题
        $this->title = '云存储管理';
        //门店
        $distributor = $this->app->db->table('lg_distributor')->order('id asc')->column('name', 'id');
        $this->assign('distributor', $distributor);

        $where = [];
        $equipment_id = input('equipment_id');
        $create_at = input('create_at');
        if ($equipment_id) {
            $where[] = ['f.equip_id', 'like', '%' . $equipment_id . '%'];
        }
        if ($create_at) {
            $create_at = explode(' - ', $create_at);
            $where[] = ['f.create_time', 'between time', $create_at];
        }
        $query = $this->_query($this->table);
        $query->alias('f');
        $query->join('lg_distributor d', 'f.shop_id = d.id', 'left');
        $query->field('f.*,d.name as shop_name')->equal('shop_id');
        $query->where($where)->order('id desc')->page();
    }

    /**
     * 添加云存储
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        $this->_applyFormToken();
        //门店
        $distributor = $this->app->db->table('lg_distributor')->order('id asc')->column('name', 'id');
        $this->assign('distributor', $distributor);

        //设备
        $equip = $this->app->db->table('lg_equip')->order('id asc')->group('equip_id')->column('name', 'equip_id');
        $this->assign('equip', $equip);

        sysoplog('云存储', '添加数据成功');
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑云存储
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->_applyFormToken();
        //门店
        $distributor = $this->app->db->table('lg_distributor')->order('id asc')->column('name', 'id');
        $this->assign('distributor', $distributor);

        //设备
        $equip = $this->app->db->table('lg_equip')->order('id asc')->group('equip_id')->column('name', 'equip_id');
        $this->assign('equip', $equip);

        sysoplog('云存储', '编辑数据成功');
        $this->_form($this->table, 'form');
    }

    protected function _form_filter(&$data)
    {
        if ($this->request->isPost()) {
            $params = $this->request->param();
            if (isset($params['id'])) {
                $data['update_time'] = time();
            } else {
                $data['create_time'] = time();
                $data['update_time'] = time();
            }
        }
    }

    public function channelArray()
    {
        $pid = input('pid');
        $where[] = ['equip_id', '=', $pid];
        $data = $this->app->db->table('lg_equip')->where($where)->order('id asc')->column('channel_name', 'channel_id');
        return json(['code' => 200, 'msg' => '获取成功', 'data' => $data]);
    }

    /**
     * 删除云存储
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $this->_applyFormToken();
        sysoplog('云存储', '删除数据成功');
        $this->_delete($this->table);
    }
}